library(survey)
library(dplyr)
library(ggplot2)

# Creating Figure 5.2 - Party differences
# Wave 1
summary(factor(mil_conf.df$Q8))
mil_conf.df$Q8a <- 0
mil_conf.df$Q8a[mil_conf.df$Q8 < 3] <- 1
summary(factor(mil_conf.df$Q8a))

mil_conf.df$DOV_AFG_WON <- NA
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_WON[mil_conf.df$DOV_ASSIGNMENT_A == 6] <- 1
summary(factor(mil_conf.df$DOV_AFG_WON))

mil_conf.df$DOV_AFG_LOST <- NA
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 1] <- 0
mil_conf.df$DOV_AFG_LOST[mil_conf.df$DOV_ASSIGNMENT_A == 5] <- 1
summary(factor(mil_conf.df$DOV_AFG_LOST))

# Wave 2
summary(factor(df$Q11))
summary(factor(df$Q11_d))

summary(factor(df$P_ASSIGN1))
df$COVID_FAIL <- NA
df$COVID_FAIL[df$P_ASSIGN1 == 1] <- 0
df$COVID_FAIL[df$P_ASSIGN1 == 2] <- 1
summary(factor(df$COVID_FAIL))

df$COVID_SUCC <- NA
df$COVID_SUCC[df$P_ASSIGN1 == 1] <- 0
df$COVID_SUCC[df$P_ASSIGN1 == 3] <- 1
summary(factor(df$COVID_SUCC))

df$NGT <- NA
df$NGT[df$P_ASSIGN1 == 1] <- 0
df$NGT[df$P_ASSIGN1 == 4] <- 1
summary(factor(df$NGT))

df$NGC <- NA
df$NGC[df$P_ASSIGN1 == 1] <- 0
df$NGC[df$P_ASSIGN1 == 5] <- 1
summary(factor(df$NGC))

df.11 <- df[!is.na(df$Q11_d),]
df.11 <- df.11[!is.na(df.11$party),]


# Creating weighted survey design objects
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df.11
  )

# Wave 1
# dov_assignment_a == 6 - won Afghanistan
AFW_d <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==0])
AFW_i <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==1])
AFW_r <- svyttest(Q8a~DOV_AFG_WON, w1_design[w1_design$variables$party==2])


# dov 5 - lost afghanistan
AFL_d <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==0])
AFL_i <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==1])
AFL_r <- svyttest(Q8a~DOV_AFG_LOST, w1_design[w1_design$variables$party==2])

# Wave 2
# P_ASSIGN1 == 2 COVID FAILURE
COVF_d <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==0])
COVF_i <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==1])
COVF_r <- svyttest(Q11_d~COVID_FAIL, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 3 COVID SUCCESS
COVS_d <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==0])
COVS_i <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==1])
COVS_r <- svyttest(Q11_d~COVID_SUCC, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 4 NG Traditional
NGTR_d <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==0])
NGTR_i <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==1])
NGTR_r <- svyttest(Q11_d~NGT, w2_design[w2_design$variables$party==2])

# P_ASSIGN1 == 5 NG Controversy
NGCO_d <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==0])
NGCO_i <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==1])
NGCO_r <- svyttest(Q11_d~NGC, w2_design[w2_design$variables$party==2])

pt.est <- c(AFW_d$estimate, AFW_i$estimate, AFW_r$estimate, 
            AFL_d$estimate, AFL_i$estimate, AFL_r$estimate,
            COVS_d$estimate, COVS_i$estimate, COVS_r$estimate, 
            COVF_d$estimate, COVF_i$estimate, COVF_r$estimate,
            NGTR_d$estimate, NGTR_i$estimate, NGTR_r$estimate,
            NGCO_d$estimate, NGCO_i$estimate, NGCO_r$estimate)
ci.low <- c(AFW_d$conf.int[1], AFW_i$conf.int[1], AFW_r$conf.int[1], 
            AFL_d$conf.int[1], AFL_i$conf.int[1], AFL_r$conf.int[1],
            COVS_d$conf.int[1], COVS_i$conf.int[1], COVS_r$conf.int[1], 
            COVF_d$conf.int[1], COVF_i$conf.int[1], COVF_r$conf.int[1],
            NGTR_d$conf.int[1], NGTR_i$conf.int[1], NGTR_r$conf.int[1],
            NGCO_d$conf.int[1], NGCO_i$conf.int[1], NGCO_r$conf.int[1])
ci.high <- c(AFW_d$conf.int[2], AFW_i$conf.int[2], AFW_r$conf.int[2], 
             AFL_d$conf.int[2], AFL_i$conf.int[2], AFL_r$conf.int[2],
             COVS_d$conf.int[2], COVS_i$conf.int[2], COVS_r$conf.int[2], 
             COVF_d$conf.int[2], COVF_i$conf.int[2], COVF_r$conf.int[2],
             NGTR_d$conf.int[2], NGTR_i$conf.int[2], NGTR_r$conf.int[2],
             NGCO_d$conf.int[2], NGCO_i$conf.int[2], NGCO_r$conf.int[2])
plot.labs <- c(" ", "Won\nAfghanistan\nWar", "  ",
               "   ", "Lost\nAfghanistan\nWar", "    ",
               "     ", "COVID\nSuccess", "      ",
               "       ", "COVID\nFailure", "        ",
               "         ", "NG\nTraditional", "          ",
               "           ", "NG\nControversial", "            ")
df.plot <- data.frame(Treatment=plot.labs, estimate=pt.est, 
                      lower=ci.low, upper=ci.high)
df.plot$Treatment <- factor(df.plot$Treatment, levels = plot.labs)

ggplot()+
  geom_pointrange(data=df.plot, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c("blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red",
                                        "blue","gray50","red"))+
  geom_hline(yintercept = 0, color = "red")+
  ylim(-.35,.35)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()
